package livesun.justiceeternal.fragment;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.List;
import livesun.justiceeternal.R;
import livesun.justiceeternal.adapter.LoveAdapter;
import livesun.justiceeternal.base.BaseFragment;
import livesun.justiceeternal.db.love.LoveBean;
import livesun.justiceeternal.db.love.LoveModule;
import livesun.justiceeternal.db.love.SimpleLoveListener;
import livesun.justiceeternal.utils.ToastUtil;
import zlc.season.practicalrecyclerview.PracticalRecyclerView;

/**
 * 类描述：关注
 * 创建人：livesun
 * 创建时间：2018/1/3.
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */
public class AttentionFragment extends BaseFragment
        implements LoveAdapter.OnItemClickListener,
        PracticalRecyclerView.OnRefreshListener {

    private PracticalRecyclerView recyclerView;
    private LoveAdapter mAdapter;

    private SimpleLoveListener simpleLoveListener = new SimpleLoveListener(){
        @Override
        public void onQueryFinish(List<LoveBean> loveBeans) {
            super.onQueryFinish(loveBeans);
            mAdapter.clear();
            mAdapter.addAll(loveBeans);
        }

        @Override
        public void onDeleteError(String error) {
            super.onDeleteError(error);
            ToastUtil.showShort("删除失败 "+error);
        }

        @Override
        public void onDeleteSucess(LoveBean loveBean) {
            super.onDeleteSucess(loveBean);
            if(mAdapter!=null){
                mAdapter.deletItme(loveBean);
                if(deletCount!=null){
                    deletCount.setText(mAdapter.getCheckedList().size()+" ");
                }
            }

        }
    };
    private View deleteLayout;
    private TextView deletCount;

    @Override
    public int getResourcesId() {
        return R.layout.fragment_attention;
    }

    @Override
    public void initView() {
        recyclerView = findView(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new LoveAdapter();
        recyclerView.setAdapterWithLoading(mAdapter);
        LoveModule.getInstance().registerListener(simpleLoveListener);
        deleteLayout = findView(R.id.delete);
        deletCount = findView(R.id.delete_count);

    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(this);
        recyclerView.setRefreshListener(this);
        LoveModule.getInstance().queryDocList();
        deleteLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<LoveBean> checkedList = mAdapter.getCheckedList();
                if(checkedList.size()==0){
                    ToastUtil.showShort("请选择要删除的条目");
                }else {
                    for (LoveBean loveBean : checkedList) {
                        LoveModule.getInstance().delete(loveBean);
                    }
                }
            }
        });
        try {
            Class aClass = recyclerView.getClass();
            Field scrool = aClass.getDeclaredField("mSwipeRefreshLayout");
            scrool.setAccessible(true);
            SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) scrool.get(recyclerView);
            mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
            mSwipeRefreshLayout.measure(0,0);
            mSwipeRefreshLayout.setRefreshing(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onRefresh() {
        LoveModule.getInstance().queryDocList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LoveModule.getInstance().unRegisterListener(simpleLoveListener);
    }

    @Override
    public void itemClick(int position, LoveBean loveBean) {
        ToastUtil.showShort(loveBean.getName());
        Intent intent = new Intent(getActivity(),LoveActivity.class);
        intent.putExtra("body",loveBean);
        startActivity(intent);
    }

    @Override
    public void onLongItemClick(int position, LoveBean loveBean) {
        deleteLayout.setVisibility(View.VISIBLE);
        changeUI(true);

        Fragment parentFragment = getParentFragment();
        if(parentFragment instanceof HomePageFragment){
            ((HomePageFragment)parentFragment).scrootoTop();
        }
    }

    @Override
    public void itemChecked(int position, LoveBean loveBean) {
        if(deletCount!=null){
            deletCount.setText(mAdapter.getCheckedList().size()+" ");
        }
    }


    public boolean goBack() {
       if(mAdapter==null){
           return false;
       }
        boolean mode = mAdapter.getMode();
       if(mode){
           mAdapter.setMode(false,-1);
           changeUI(false);
           if(deletCount!=null){
               deletCount.setText("0");
           }
           return true;
       }
      return false;
    }

    private void changeUI(boolean b) {
        if(b){
            deleteLayout.setVisibility(View.VISIBLE);
        }else {
            deleteLayout.setVisibility(View.GONE);
        }
    }
}
